﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Manager_Banners : System.Web.UI.PageEx
{
    private string strPageCode = "Q0000C";
    private string strLoginResult = string.Empty;

    private int iPageCount = 0;
    private int iRecordCount = 0;
    private string strQuery = string.Empty;
    private int iSelectId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!DalEx.DbManagerEx.CheckLoginRight(UserType, UserId, strPageCode, ref strLoginResult))
        {
            FineUI.Alert.Show(strLoginResult, FineUI.MessageBoxIcon.Warning);
            Response.Redirect("Login.aspx");
        }
        if (!Page.IsPostBack)
        {
            DataBinds();
            btnReset.OnClientClick = sfObject.GetResetReference();
        }
    }
    private void DataBinds()
    {
        DataSet ds = WebBase.Comm.SelectByPage("dbBanner", "*", strQuery, "BannerId", "BannerId", 0, 1, gridList.PageSize, gridList.PageIndex + 1, out iPageCount, out iRecordCount);
        gridList.RecordCount = iRecordCount;
        gridList.DataSource = ds;
        gridList.DataBind();
    }
    protected void gridList_PageIndexChange(object sender, FineUI.GridPageEventArgs e)
    {
        gridList.PageIndex = e.NewPageIndex;
        DataBinds();
    }
    protected void gridDataList_RowDataBound(object sender, FineUI.GridRowEventArgs e)
    {
        DataRowView drv = (DataRowView)gridList.Rows[e.RowIndex].DataItem;
        int iWhere = Convert.ToInt32(drv["IsWhere"]);
        if (drv != null)
        {
            e.Values[2] = Convert.ToString((BannerType)iWhere);
        }
    }
    protected void gridList_RowDoubleClick(object sender,EventArgs e)
    {
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        if (lstSelectRowId.Count >= 1)
        {
            iSelectId = lstSelectRowId[0];
            Model.DbBanner dbBanner = DalEx.DbBannerEx.Select(iSelectId);
            if (dbBanner != null)
            {
                hfBannerId.Text = dbBanner.BannerID.ToString();
                ddlAdvType.SelectedValue = dbBanner.IsWhere.ToString();
                txtTitle.Text = dbBanner.BannerName;
                txtUrl.Text = dbBanner.LinkUrl;
                txtOrderNum.Text = dbBanner.OrderNum.ToString();
                cboxStatus.Checked = Convert.ToBoolean(dbBanner.Status);
            }
        }
    }
    protected void btnSelectDelete_Click(object sender, EventArgs e)
    {
        string strResult = string.Empty;
        List<int> lstSelectRowId = WebBase.Comm.GetGridSeleteRowId(gridList);
        if (lstSelectRowId.Count >= 1)
        {
            string[] SelectIdArray = new string[lstSelectRowId.Count];
            int iIndexTemp =0;
            foreach (int iLinkId in lstSelectRowId)
            {
                SelectIdArray[iIndexTemp] = iLinkId.ToString();
                iIndexTemp++;
            }
            if (DalEx.DbBannerEx.Delete(" and BannerId in (" + string.Join(",", SelectIdArray) + ")", ref strResult) > 0)
            {
                FineUI.Alert.Show(strResult, "信息提示", FineUI.MessageBoxIcon.Information);
                sfObject.GetResetReference();
                DataBinds();
            }
            else
            {
                FineUI.Alert.Show(strResult, "信息提示", FineUI.MessageBoxIcon.Error);
            }
        }
        else
        {
            FineUI.Alert.Show("请选择要删除的内容！", "信息提示", FineUI.MessageBoxIcon.Warning);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int iOrderNum = 0;
        int IsWhere = 0;
        int.TryParse(hfBannerId.Text.Trim(), out iSelectId);
        string strTitle = SqlHelper.String(txtTitle.Text);
        string strWhere = ddlAdvType.SelectedValue;
        int.TryParse(strWhere, out IsWhere);
        int.TryParse(SqlHelper.String(txtOrderNum.Text), out iOrderNum);
        string strUrl = SqlHelper.String(txtUrl.Text);
        string strAdvPicPath = string.Empty;
        bool bStatus = cboxStatus.Checked;

        string strErrorMsg = string.Empty;
        strAdvPicPath = WebBase.Comm.GetFilePath(fuAdvImg, Server.MapPath("~/upload/AdvFiles/"), ResolveUrl("~/upload/AdvFiles/"), false, 1, AllowUpFileExt, ref strErrorMsg);

        Model.DbBanner dbBanner = new Model.DbBanner();
        dbBanner.AddTime = DateTime.Now;
        dbBanner.BannerUrl = strAdvPicPath;
        if (iSelectId > 0)
        {
            dbBanner = DalEx.DbBannerEx.Select(iSelectId);
            strAdvPicPath = string.IsNullOrEmpty(strAdvPicPath) ? dbBanner.BannerUrl : strAdvPicPath;
        }
        else
        {
            if (string.IsNullOrEmpty(strAdvPicPath))
            {
                FineUI.Alert.Show("请选择广告&焦点图片！", "信息提示", FineUI.MessageBoxIcon.Error);
                return;
            }
        }
        dbBanner.BannerName = strTitle;
        dbBanner.IsWhere = IsWhere;
        dbBanner.LinkUrl = strUrl;
        dbBanner.OrderNum = iOrderNum;
        dbBanner.Status = Convert.ToInt32(bStatus);
        //dbBanner. = sType;
        dbBanner.BannerUrl =strAdvPicPath;

        string strResult = string.Empty;
        if (DalEx.DbBannerEx.Insert(dbBanner, ref strResult) > 0)
        {
            DataBinds();
            FineUI.Alert.Show(strResult, "信息提示", FineUI.MessageBoxIcon.Information);
        }
        else
        {
            FineUI.Alert.Show(strResult, "信息提示", FineUI.MessageBoxIcon.Error);
        }
    }

    protected string GetIsWhere(object objValue)
    {
        string strResult = string.Empty;
        int iValue = Convert.ToInt32(objValue);
        switch (iValue)
        {
            case 1:
                strResult = "首页Banner广告";
                break;
            case 2:

                strResult = "荣誉证书展播";
                break;
        }
        return strResult;
    }
}
